﻿using System;
using System.Net;
using Ninject;
using Ninject.Syntax;
using Ninject.Modules;
using WebSiteAdminTool.Common.Ioc;
using WebSiteAdminTool.Web.Ioc.Mappings;
using WebSiteAdminTool.Web.DataAccess.Interfaces;
using WebSiteAdminTool.Web.ConfigManager.Interfaces;
using System.Web;
using WebSiteAdminTool.Common.Utils;

namespace WebSiteAdminTool.UI.Ioc
{
    public class ServiceLocator : BaseLocator
    {
        #region instance

        private static ServiceLocator _instance = null;

        #endregion

        #region public props

        public static ServiceLocator Instance
        {
            get
            {
                return _instance;
            }
        }

        #endregion

        #region ctors

        static ServiceLocator()
        {
            _instance = new ServiceLocator();
        }

        public ServiceLocator()
            : base(GetApplicationMode())
        { }

        #endregion

        #region initialization

        protected override INinjectModule GetDesignTimeBindings()
        {
            // TODO: implement
            return GetRunTimeBindings();
        }

        protected override INinjectModule GetRunTimeBindings()
        {
            return new RunTimeMappings();
        }

        protected override INinjectModule GetTestTimeBindings()
        {
            return new TestingMappings();
        }

        #endregion

        #region helpers

        private static Constants.ApplicationModes GetApplicationMode()
        {
            if (HttpContext.Current == null)
                return Constants.ApplicationModes.Design;

            // TODO: add testing mode check
            return Constants.ApplicationModes.RunTime;
        }

        #endregion

        #region object accessors

        // data access
        public IMembershipDataAccess MembershipDataAccess { get { return base.GetObject<IMembershipDataAccess>(); } }
        public IRolesDataAccess RolesDataAccess { get { return base.GetObject<IRolesDataAccess>(); } }
        // config managers
        public IMembershipConfigManager MembershipConfigManager { get { return base.GetObject<IMembershipConfigManager>(); } }
        public IRolesConfigManager RolesConfigManager { get { return base.GetObject<IRolesConfigManager>(); } }

        #endregion
    }
}
